package com.pirestupppai.ui.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import com.blog.www.guideview.Component;
import com.blog.www.guideview.Guide;
import com.blog.www.guideview.GuideBuilder;
import com.pirestupppai.R;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.common.Constant;
import com.pirestupppai.imagetool.copytool.CopyView;
import com.pirestupppai.utils.GuideComponent;
import com.pirestupppai.utils.SharedPreferenceUtil;

import butterknife.BindView;

/**
 *  仿制页面
 */

public class ImageCopyActivity extends BaseActivity {

    @BindView(R.id.copy_view)
    CopyView mCopyView;
    @BindView(R.id.get_target_layout)
    RelativeLayout mGetTargetLayout;
    @BindView(R.id.copy_layout)
    RelativeLayout mCopyLayout;
    @BindView(R.id.complete_btn)
    ImageView mCompleteBtn;
    @BindView(R.id.back_btn)
    ImageView mBackBtn;
    @BindView(R.id.seekbar)
    SeekBar mCopySizeSeekBar;
    @BindView(R.id.get_target_btn)
    ImageView mGetTargetBtn;
    @BindView(R.id.copy_btn)
    ImageView mCopyBtn;

    private Guide mGuideOperation;

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected void initListeners() {
        mGetTargetLayout.setOnClickListener(this);
        mCopyLayout.setOnClickListener(this);
        mCompleteBtn.setOnClickListener(this);
        mBackBtn.setOnClickListener(this);

        mCopySizeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float radiusSize = (((float)progress / 50f)) * 100f;
                mCopyView.setCircleSize((int) radiusSize);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_image_copy;
    }

    @Override
    protected void initLogic() {
        mCopyView.setBackgroundBitmap(ImageEditActivity.mDeliverBitmap);

        SharedPreferences sp = SharedPreferenceUtil.getSp();
        SharedPreferences.Editor editor = SharedPreferenceUtil.getEditor();

        boolean isFirstRun = sp.getBoolean(Constant.SPKey.FIRSTIMAGECOPYACTIVITY, true);
        if (isFirstRun) {
            editor.putBoolean(Constant.SPKey.FIRSTIMAGECOPYACTIVITY, false);
            editor.apply();
            // 引导层
            mGetTargetBtn.post(new Runnable() {
                @Override
                public void run() {
                    ShowGetTargetGuideView();
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.get_target_layout:
                mCopyView.CopyStatus();
                mGetTargetBtn.setSelected(true);
                mCopyBtn.setSelected(false);
                break;
            case R.id.copy_layout:
                mCopyView.PasterStatus();
                mGetTargetBtn.setSelected(false);
                mCopyBtn.setSelected(true);
                break;
            case R.id.complete_btn:
                ImageEditActivity.mDeliverBitmap = mCopyView.getmBackgroundBitmap();
                setResult(RESULT_OK);
                this.finish();
                break;
            case R.id.back_btn:
                this.finish();
                break;
        }
    }

    public void ShowGetTargetGuideView() {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(mGetTargetBtn)
                .setAlpha(150)
                .setHighTargetCorner(20)
                .setHighTargetPaddingTop(10)
                .setHighTargetPaddingLeft(30)
                .setHighTargetPaddingRight(30)
                .setHighTargetPaddingBottom(35)
                .setOverlayTarget(false)
                .setOutsideTouchable(false);
        builder.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {
            }

            @Override
            public void onDismiss() {
                ShowCopyGuideView();
            }
        });
        builder.addComponent(new GuideComponent(mContext, "点击获取图章目标区域", Component.ANCHOR_TOP, Component.FIT_START, -50, -50));
        mGuideOperation = builder.createGuide();
        mGuideOperation.setShouldCheckLocInWindow(false);
        mGuideOperation.show(ImageCopyActivity.this);
    }

    public void ShowCopyGuideView() {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(mCopyBtn)
                .setAlpha(150)
                .setHighTargetCorner(20)
                .setHighTargetPaddingTop(10)
                .setHighTargetPaddingLeft(30)
                .setHighTargetPaddingRight(30)
                .setHighTargetPaddingBottom(35)
                .setOverlayTarget(false)
                .setOutsideTouchable(false);
        builder.addComponent(new GuideComponent(mContext, "点击仿制目标区域", Component.ANCHOR_TOP, Component.FIT_END, 50, -50));
        mGuideOperation = builder.createGuide();
        mGuideOperation.setShouldCheckLocInWindow(false);
        mGuideOperation.show(ImageCopyActivity.this);
    }
}
